/**
 * 
 */
package com.rc.model;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.net.Uri;
import android.util.Log;

import com.rc.activity.dao.tbl_Category;
import com.rc.activity.dao.tbl_Merchant;
import com.rc.activity.dao.tbl_Receipt;
import com.rc.activity.dao.tbl_Report;
import com.rc.activity.dao.tbl_Settings;
import com.rc.activity.dao.tbl_Trip;
import com.rc.activity.dao.tbl_TripDao;
import com.rc.activity.dao.tbl_TripItems;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.net.NetSupport;
import com.rc.object.BaseObject;
import com.rc.object.CategoryObj;
import com.rc.object.ItemsObj;
import com.rc.object.MerchantObj;
import com.rc.object.ReceiptObj;
import com.rc.object.ReportObj;
import com.rc.object.SettingsObj;
import com.rc.object.TripItemsObj;
import com.rc.object.TripObj;
import com.telpoo.frame.model.BaseTask;
import com.telpoo.frame.model.TaskListener;
import com.telpoo.frame.model.TaskParams;

public class Model extends BaseTask implements TaskType {

	public Model(TaskListener taskListener, int taskType, ArrayList<?> list,
			Context context) {
		super(taskListener, taskType, list, context);
	}

	@Override
	protected Boolean doInBackground(TaskParams... params) {
		String limit = "";
		if (taskListener == null)
			return TASK_FAILED;
		switch (taskType) {
		case TASK_LOGIN:
			String UserName = params[0].getStringParams()[0];
			String PassWord = params[0].getStringParams()[1];
			return Login(UserName, PassWord);
		case TASK_CHANGE_PASS:
			String NewPassword = params[0].getStringParams()[1];
			String NewPasswordConfirm = params[0].getStringParams()[2];
			String Password = params[0].getStringParams()[0];
			return change_pass(Password, NewPassword, NewPasswordConfirm);
		case TASK_GET_RECEIPT:
			return getReceipt();
		case TASK_GET_REPORT:
			limit = params[0].getStringParams()[0];
			String tripCount = params[0].getStringParams()[1];
			return getReport(limit, tripCount);
		case TASK_GET_MERCHANT:
			return getMerchants();
		case TASK_GET_TRIP:
			return getTrip();
		case TASK_GET_CATEGORYS:
			return getCategory();
		case TASK_GET_ITEMS:
			return getItemByReceipt();
		case TASK_GET_TRIP_ITEMS:
			return getTripItem();
		case TASK_SETTINGS:
			return getSetting();
		case TASK_REGISTER:
			String firstName = params[0].getStringParams()[0];
			String lastName = params[0].getStringParams()[1];
			String email = params[0].getStringParams()[2];
			String password = params[0].getStringParams()[3];
			String passwordConfirm = params[0].getStringParams()[4];
			return register(firstName, lastName, email, password,
					passwordConfirm);
		case TASK_TRIP_TO_REPORT:
			String mTripID = params[0].getStringParams()[0];
			String mDate = params[0].getStringParams()[1];
			return tripToReport(mTripID, mDate);
		case TASK_GET_CURRENCY_CONVERTER:
			String fromCode = params[0].getStringParams()[0];
			String toCode = params[0].getStringParams()[1];
			String purchaseDate = params[0].getStringParams()[2];
			return getCurrencyConvert(fromCode, toCode, purchaseDate);
		case TASK_DELETE_DEVICE:
			String device = params[0].getStringParams()[0];
			return NetSupport.delete_device(device, (Activity) context);
		case TASK_FEED_BACK:
			String fullName = params[0].getStringParams()[0];
			email = params[0].getStringParams()[1];
			String message = params[0].getStringParams()[2];
			String subject = params[0].getStringParams()[3];
			return api_contact(fullName, email, message, subject);
		case TASK_GET_TRIP_FREE:
			return getTripFree();
		case TASK_GET_RECEIPT_FREE:
			return get_ReceiptFree();
		case TASK_GET_ITEM_FREE:
			String tripID = params[0].getStringParams()[0];
			return validateTripID(tripID);
		case TASK_GET_TRIPITEM_PENDING:
			return getTripItemPending();
		case TASK_GET_ITEM_PENDING:
			return getItemPending();
		}
		if ((dataReturn != null && dataReturn.size() > 0)) {
			return TASK_DONE;
		}
		return TASK_FAILED;

	}

	private Boolean get_ReceiptFree() {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
		arrReceipt = DatabaseSupport.getAllReceipt(context);
		arrBaseObject = NetSupport.get_ReceiptFree(arrReceipt, 30);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Receipt objReceipt = new tbl_Receipt();
			objReceipt = ReceiptObj.getReceiptbyBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_RECEIPT,
					objReceipt.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean validateTripID(String tripID) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.validateTripID(tripID, context);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Receipt objReceipt = new tbl_Receipt();
			objReceipt = ReceiptObj.getReceiptbyBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_RECEIPT,
					objReceipt.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getItemByReceipt() {
		ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
		Cursor cursor = context.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT, null, null, null, null);
		arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_Items(arrReceipt);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			context.getContentResolver()
					.insert(Contract.CONTENT_URI_ITEMS,
							ItemsObj.getObjItemByBaseObject(objBase)
									.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getItemPending() {

		ArrayList<tbl_Trip> arrTrips = new ArrayList<tbl_Trip>();
		arrTrips = DatabaseSupport.getTripsPending(context);
		if (arrTrips == null) {
			return true;
		}
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_ItemsPending(arrTrips);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			context.getContentResolver()
					.insert(Contract.CONTENT_URI_ITEMS,
							ItemsObj.getObjItemByBaseObject(objBase)
									.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean api_contact(String fullName, String email, String message,
			String subject) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.api_Contact(fullName, email, message,
				subject);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			context.getContentResolver()
					.insert(Contract.CONTENT_URI_ITEMS,
							ItemsObj.getObjItemByBaseObject(objBase)
									.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getCurrencyConvert(String fromCode, String toCode,
			String purchaseDate) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_CurrencyConverter(fromCode, toCode,
				purchaseDate);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getSetting() {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_setting();
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Settings objSetting = new tbl_Settings();
			objSetting = SettingsObj.getReceiptbyBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_SETTING,
					objSetting.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getTripFree() {
		// get report
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_Trip_free(20, context);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Trip objTrip = new TripObj();
			objTrip = TripObj.getObjTripByBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_TRIP,
					objTrip.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean register(String firstName, String lastName, String email,
			String password, String passwordConfirm) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.api_RegisterUser(firstName, lastName, email,
				password, passwordConfirm);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		} else if (arrBaseObject.size() == 0) {
			return TASK_FAILED;
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean Login(String UserName, String PassWord) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.api_Login(UserName, PassWord);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		} else if (arrBaseObject.size() == 0) {
			return TASK_FAILED;
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean change_pass(String Password, String NewPassword,
			String NewPasswordConfirm) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.change_pass(Password, NewPassword,
				NewPasswordConfirm, context);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String status = baseObj.getParam("status");
			if ("success".equals(status)) {
				return TASK_DONE;
			} else {
				msg = baseObj.getParam("message");
				if (msg == null) {
					return TASK_DONE;
				}
				msg = msg.replace("[", "");
				msg = msg.replace("]", "");
				msg = msg.replace("\"", "");
				return TASK_FAILED;
			}
		} else if (arrBaseObject.size() == 0) {
			return TASK_FAILED;
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean tripToReport(String mTripID, String mDate) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.api_TripToReport(mTripID, mDate);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		} else if (arrBaseObject.size() == 0) {
			return TASK_FAILED;
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Report objReport = new ReportObj();
			objReport = ReportObj.getReportbyBaseObject(objBase);
			Uri uri = context.getContentResolver().insert(
					Contract.CONTENT_URI_REPORT, objReport.getContentValues());
			tbl_Trip objTrip = new tbl_Trip();
			objTrip = DatabaseSupport.getTripByID(Long.valueOf(mTripID),
					context);
			objTrip.setReport_ID(Long.valueOf(uri.getLastPathSegment()));
			objTrip.setIsUpdate("1");
			objTrip.setIsClaimed(1);
			context.getContentResolver().update(Contract.CONTENT_URI_TRIP,
					objTrip.getContentValues(),
					tbl_TripDao.Properties.TripID.columnName + " = " + mTripID,
					null);
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getReceipt() {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		ArrayList<tbl_Trip> arrTrip = new ArrayList<tbl_Trip>();
		Cursor cursor = context.getContentResolver().query(
				Contract.CONTENT_URI_TRIP, null, null, null, null);
		arrTrip = tbl_Trip.getObjectbyCursor(cursor);
		arrBaseObject = NetSupport.get_Receipt(arrTrip);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Receipt objReceipt = new tbl_Receipt();
			objReceipt = ReceiptObj.getReceiptbyBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_RECEIPT,
					objReceipt.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getReport(String limit, String tripCount) {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_Report(limit, tripCount, context);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Report objReport = new ReportObj();
			objReport = ReportObj.getReportbyBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_REPORT,
					objReport.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getTrip() {
		// get report
		ArrayList<tbl_Report> arrReport = new ArrayList<tbl_Report>();
		Cursor cursor = context.getContentResolver().query(
				Contract.CONTENT_URI_REPORT, null, null, null, null);
		arrReport = tbl_Report.getObjectbyCursor(cursor);
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_Trip(arrReport);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Trip objTrip = new TripObj();
			objTrip = TripObj.getObjTripByBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_TRIP,
					objTrip.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getTripItem() {
		ArrayList<tbl_Receipt> arrReceipt = new ArrayList<tbl_Receipt>();
		Cursor cursor = context.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT, null, null, null, null);
		arrReceipt = tbl_Receipt.getObjectbyCursor(cursor);
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_TripItems(arrReceipt);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_TripItems objTripItems = new tbl_TripItems();
			objTripItems = TripItemsObj.getObjTripByBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_TRIP_ITEM,
					objTripItems.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getTripItemPending() {
		ArrayList<tbl_Trip> arrTrips = new ArrayList<tbl_Trip>();
		arrTrips = DatabaseSupport.getTripsPending(context);
		if (arrTrips == null) {
			return true;
		}
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_TripItemsPeding(arrTrips);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_TripItems objTripItems = new tbl_TripItems();
			objTripItems = TripItemsObj.getObjTripByBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_TRIP_ITEM,
					objTripItems.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getMerchants() {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		ArrayList<tbl_Receipt> arrReceipts = new ArrayList<tbl_Receipt>();
		Cursor cursor = context.getContentResolver().query(
				Contract.CONTENT_URI_RECEIPT, null, null, null, null);
		arrReceipts = tbl_Receipt.getObjectbyCursor(cursor);
		arrBaseObject = NetSupport.get_Merchants(arrReceipts);
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}

		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Merchant objMerchant = new tbl_Merchant();
			objMerchant = MerchantObj.getObjMerchantByBaseObject(objBase);
			try {
				context.getContentResolver().insert(
						Contract.CONTENT_URI_MERCHANT,
						objMerchant.getContentValues());
			} catch (SQLiteConstraintException e) {
				// TODO: handle exception
				e.printStackTrace();
				Log.e("Insert Merchant", e.getMessage());
			}
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

	private Boolean getCategory() {
		ArrayList<BaseObject> arrBaseObject = new ArrayList<BaseObject>();
		arrBaseObject = NetSupport.get_Category();
		if (arrBaseObject == null) {
			msg = "connect error";
			return TASK_FAILED;
		} else if (arrBaseObject.size() == 1) {
			BaseObject baseObj = new BaseObject();
			baseObj = arrBaseObject.get(0);
			String error = baseObj.getParam("message");
			if (!"".equals(error) && error != null) {
				msg = baseObj.getParam("message");
				return TASK_FAILED;
			}
		}
		for (int i = 0; i < arrBaseObject.size(); i++) {
			BaseObject objBase = new BaseObject();
			objBase = arrBaseObject.get(i);
			tbl_Category objTrip = new tbl_Category();
			objTrip = CategoryObj.getObjTripByBaseObject(objBase);
			context.getContentResolver().insert(Contract.CONTENT_URI_CATEGORY,
					objTrip.getContentValues());
		}
		msg = "success";
		dataReturn = arrBaseObject;
		return TASK_DONE;
	}

}
